package com.huatai.exchange.deal;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.integration.base.service.FileOperateService;
import com.huatai.exchange.integration.vba.service.FundInfoVoService;
import com.huatai.exchange.service.ExcFileLoadInfoService;
import com.huatai.exchange.service.ExcFileLoadRecordService;
import com.huatai.exchange.utils.FileDealUtils;
import com.huatai.exchange.utils.GzFundUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

public abstract class AbstractFileCreateDealServiceImpl {

    protected static final Logger logger = LoggerFactory.getLogger(FileCreateDealService.class);

    @Value("${tmp_file_path}")
    protected String tmpFile;

    @Autowired
    protected FundInfoVoService fundInfoVoService;

    @Autowired
    protected FileOperateService fileOperateService;

    @Autowired
    protected ExcFileLoadRecordService excFileLoadRecordService;

    @Autowired
    protected ExcFileLoadInfoService excFileLoadInfoService;

    @Autowired
    protected GzFundUtils gzFundInfoUtils;

    protected String formatStr(String str, Map<String, Object> resvMap) throws BusinessException {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String tmpStr = str;
        String ztbh = (String) resvMap.get(Contents.FIELD_ZTBH);
        String yyyyMMdd = (String) resvMap.get(Contents.FIELD_YYYYMMDD);
        while (tmpStr.contains("${") && tmpStr.contains("}")) {
            Integer startNum = tmpStr.indexOf("${") + 2;
            Integer endNum = tmpStr.indexOf("}");
            String field = tmpStr.substring(startNum, endNum);
            logger.info("field:" + field);
            String value = (String) resvMap.get(field);
            if (StringUtils.isBlank(value)) {
                switch (field) {
                    case Contents.FIELD_ZTMC:
                        value = gzFundInfoUtils.getFundName(ztbh);
                        break;
                    case Contents.FIELD_YYMMDD:
                        value = yyyyMMdd.substring(2);
                        break;
                    case Contents.FIELD_YYYY:
                        value = yyyyMMdd.substring(0, 4);
                        break;
                    case Contents.FIELD_YYYYMM:
                        value = yyyyMMdd.substring(0, 6);
                        break;
                    case Contents.FIELD_YYMM:
                        value = yyyyMMdd.substring(2, 6);
                        break;
                    case Contents.FIELD_YYYY_MM_DD:
                        value = DateFormatUtils.transfer(yyyyMMdd);
                        break;
                    case Contents.FIELD_MM_DD:
                        value = yyyyMMdd.substring(4, 6) + "-" + yyyyMMdd.substring(6, 8);
                        break;
                    case Contents.FIELD_MCDD:
                        value = FileDealUtils.transferMonth(yyyyMMdd.substring(4, 6)) + yyyyMMdd.substring(6, 8);
                        break;
                    default:
                        value = "";
                        break;
                }
                if(StringUtils.isBlank(value)) {
                    throw new BusinessException("未识别标志["+field+"]") ;
                }
                resvMap.put(field, value);
            }
            String oldStr = "${" + field + "}";
            tmpStr = tmpStr.replace(oldStr, value);
        }
        return tmpStr;
    }
}
